#include "deletefile.h"
#include "file/fdebug.h"

deletefile::deletefile()
{
//    timer =  new QTimer(this);
//    connect(timer, SIGNAL(timeout()), this, SLOT(autoDelete()));

}

void deletefile::parameterSet(SOFTWARESETTINGS newsettings)
{
    fileSetting = newsettings;
    timeline = fileSetting.timeline;

    //QDBG << " timeline" << timeline;
    //QDBG << " fileSetting.deletetimeMode" << fileSetting.deletetimeMode;
    //QDBG << "fileSetting.dataFilePath" << fileSetting.dataFilePath;

    switch (fileSetting.deletetimeMode)
    {
    case 0:
        deletetime = 60 * 60 * 24 * 30 * 12;
        break;
    case 1:
        deletetime = 60 * 60 * 24 * 30;
        break;
    case 2:
        deletetime = 60 * 60 * 24;
        break;
    case 3:
        deletetime = 60 * 60;
        break;
    case 4:
        deletetime = 1;
        break;
    default:
        break;
    }
}

void deletefile::FolderPaths()
{
    QString path = fileSetting.dataFilePath;
    if (fileSetting.autoCreateDateDir)
        path = path.left(path.length() - 9);
    //QDBG << "path" << path;

    //this->parameterSet();
    QDir dir(path);
    if (!dir.exists())
        return;
    dir.setFilter(QDir::Dirs);//| QDir::Files | QDir::NoSymLinks
    QFileInfoList list = dir.entryInfoList();
    int file_count = list.count();
    if (file_count <= 0)
        return;
    for (int i = 2; i < file_count; i++)
    {
        QFileInfo info = list.at(i);
        {
            if (info.exists())
            {
                qint64 dt;
                QDateTime currentTime = QDateTime::currentDateTime();
                dt = (info.created()).secsTo(currentTime);
                double timelag = double(dt);
                timelag = timelag / (deletetime);
                if (timelag > timeline && timeline > 0)
                {
                    //QDBG << "timeline" << timeline;
                    strPath = info.absoluteFilePath();
                    this->DeleteFileOrFolder(strPath);
                }
                //QDBG << "timelag" << timelag;
            }
        }
    }
}

inline bool deletefile::DeleteFileOrFolder( const QString &strPath )
{
    if ( strPath.isEmpty() || !QDir().exists( strPath ) )
        return false;

    QFileInfo fileInfo( strPath );

    if ( fileInfo.isFile() )
        QFile::remove( strPath );
    else if ( fileInfo.isDir() )
    {
        QDir qDir( strPath );
        qDir.setFilter( QDir::AllEntries | QDir::NoDotAndDotDot );
        QFileInfoList fileInfoLst = qDir.entryInfoList();
        foreach ( QFileInfo qFileInfo, fileInfoLst )
        {
            if ( qFileInfo.isFile() )
                qDir.remove( qFileInfo.absoluteFilePath() );
            else
            {
                DeleteFileOrFolder( qFileInfo.absoluteFilePath() );
                qDir.rmdir( qFileInfo.absoluteFilePath() );
            }
        }
        qDir.rmdir( fileInfo.absoluteFilePath() );
    }

    return true;
}

void deletefile::autoDelete()
{
    //timeSet = fileSetting.timehours + ":" + fileSetting.timemins;
    timeSet = fileSetting.deletetime.toString("hh:mm");
    //QDBG << "timeset" << timeSet;
    QDateTime datetime = QDateTime::currentDateTime();
    if (datetime.toString("hh:mm") == timeSet)
        FolderPaths();
//    timer->start(1000*10);
}

QDateTime deletefile::DeleteTimeline()
{
    QDateTime dTime = QDateTime::currentDateTime();

    dTime = dTime.addSecs((-(deletetime * timeline)));
    return dTime;
}
